package com.example.itshome.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.itshome.R;
import com.example.itshome.adapter.ShopPageRecycleViewAdapter;
import com.example.itshome.entity.CommodityEntity;
import com.example.itshome.impl.ItemDecorationImpl;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.AbstractRecycleVIewGetRequest;
import com.example.itshome.network.GetCollectionCommodityRequest;
import com.example.itshome.network.GetCommodityRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class ShopItemFragment extends Fragment {

    public RecyclerView mRecycleView;
    public List<CommodityEntity> mRecycleViewData;
    private CommodityPageType pageType;
    public enum CommodityPageType{
        Collection,All,Dog,Cat,Other
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_shop_page_item, container, false);
        Bundle bundle=getArguments();
        if(bundle!=null){
            pageType=CommodityPageType.valueOf(bundle.getString("pageType"));
        }
        initFind(view);
        initRecycleView();
        return view;
    }


    void initFind(View v){
        mRecycleView=v.findViewById(R.id.rcv_shop_page);
    }

    void initRecycleView(){
        mRecycleViewData=new ArrayList<>();
        mRecycleView.setAdapter(new ShopPageRecycleViewAdapter(getContext(),mRecycleViewData));
        mRecycleView.setLayoutManager(new GridLayoutManager(getContext(),2));
        mRecycleView.addItemDecoration(new ItemDecorationImpl(getContext(),10,10));

        AbstractRecycleVIewGetRequest<CommodityEntity> request=null;
        switch (pageType){
            case Collection:request=new GetCollectionCommodityRequest(getActivity());break;
            case Cat:request=new GetCommodityRequest(getActivity()).setCategory(GetCommodityRequest.CAT_CATEGORY);break;
            case Dog:request=new GetCommodityRequest(getActivity()).setCategory(GetCommodityRequest.DOG_CATEGORY);break;
            case All:request=new GetCommodityRequest(getActivity()).setCategory(GetCommodityRequest.ALL_CATEGORY);break;
            case Other:request=new GetCommodityRequest(getActivity()).setCategory(GetCommodityRequest.OTHER_CATEGORY);break;
        }
        request.setAdapter(mRecycleView.getAdapter());
        request.setCurrentData(mRecycleViewData);
        request.start();
    }

}